$(function () {
    $("#jqGrid").Grid({
        url: '../devalarmbool/list',
        rownumWidth: 60,
        colModel: [
            {label: '设备底层码', name: 'devLastCode', index: 'DEV_LAST_CODE', width: 80},
            {label: '报警代码', name: 'code', index: 'CODE', width: 80},
            {label: '报警地址', name: 'alarmAddr', index: 'ALARM_ADDR', width: 80},
            {label: '报警信息', name: 'msg', index: 'MSG', width: 80},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        devAlarmBool: {alarmAddr: '', devLastCode: '', msg: '', code: '', id: ''},
        ruleValidate: {
            devLastCode: [
                {required: true, message: '低层设备码必填', trigger: 'blur'}
            ],
            alarmAddr: [
                {required: true, message: '报警地址必填', trigger: 'blur'}
            ],
            msg: [
                {required: true, message: '报警信息必填', trigger: 'blur'}
            ],
            code: [
                {required: true, message: '报警代码必填', trigger: 'blur'}
            ]
        },
        q: {
            devLastCode: '',
            code: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.devAlarmBool = {alarmAddr: '', devLastCode: '', delFlag: '', msg: '', code: '', devId: '', id: '',};
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.devAlarmBool.id == (null || '') ? "../devalarmbool/save" : "../devalarmbool/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.devAlarmBool),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../devalarmbool/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../devalarmbool/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.devAlarmBool = r.devAlarmBool;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                devLastCode: '',
                code: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传.xls或.xlsx的文件。'
            });
        },
        handleBeforeUpload: function () {
            this.$refs.uploadFile.clearFiles();
            this.$Loading.start();
        },
        handleSuccess: function (res, file) {
            if (res.code == 0) {
                this.$Loading.finish();
                iview.Message.success('文件上传成功');
                alert(res.msg, function (index) {
                    vm.reload();
                });
            } else {
                this.$Loading.error();
                this.$Notice.error({
                    title: '导入失败',
                    desc: res.msg
                })
            }
        },
        handleError: function (err, file) {
            alert('导入失败');
        }
    }
});